package cn.shop.merchant.controller.gametemplate;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.model.gameconfig.GameTemplate;
import cn.shop.merchant.req.GameTemplateQueryReq;
import cn.shop.merchant.service.GameInfoService;
import cn.shop.merchant.service.GameTemplateService;
import cn.shop.merchant.service.MerchantUserService;
import cn.shop.transcation.merchantRateModule.entity.MerchantRate;
import cn.shop.transcation.merchantUser.mapper.entity.MerchantUser;
import cn.shop.utils.ApiResult;
import cn.shop.utils.Putif;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import cn.shop.utils.enums.Status;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.EnumUtil.getEnum;
import static cn.shop.utils.constant.Constant.UID;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-2 13:07
 * @description : 游戏模板配置
 */
@RestController
@RequestMapping("/game/template")
public class GameTemplateController {

    @Resource
    private GameTemplateService templateService;
    @Resource
    private GameInfoService gameInfoService;
    @Resource
    private MerchantUserService merchantUserService;

    /**
     * 获取游戏模板列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("/list")
    public ApiResult<Map<String, Object>> list(GameTemplateQueryReq req) throws Exception {
        long uid = StpUtil.getSession().getLong(UID);
        Map<String, Object> data = templateService.findConfigList(uid, req);
        return ApiResult.success(data);
    }

    @GetMapping("/all")
    public ApiResult<List<Map<String, Object>>> allList() {
        long uid = StpUtil.getSession().getLong(UID);
        List<Map<String, Object>> data = templateService.allList(uid);
        return ApiResult.success(data);
    }

    /**
     * 保存或修改游戏模板
     *
     * @param template 游戏模板内容
     * @return 返回结果
     */
    @PostMapping("")
    @OperationLog(desc = "新增游戏模板", type = OperationType.CREATE)
    public ApiResult<Map<String, Object>> insert(@Validated @RequestBody GameTemplate template) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("用户编号为空，请检查登录状态");
        }
        MerchantRate rate = merchantUserService.getRate(uid);
        if (rate.getTemplateFloat().compareTo(template.getCharge()) < 0) {
            return ApiResult.fail("模板手续费不能超过" + rate.getTemplateFloat() + "%");
        }
        boolean isSuccess = templateService.save(template);
        return ApiResult.result(isSuccess);
    }

    /**
     * 保存或修改游戏模板
     *
     * @param template 游戏模板内容
     * @return 返回结果
     */
    @PutMapping("")
    @OperationLog(desc = "修改游戏模板", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> update(@Validated @RequestBody GameTemplate template) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("用户编号为空，请检查登录状态");
        }
        MerchantRate rate = merchantUserService.getRate(uid);
        if (rate.getTemplateFloat().compareTo(template.getCharge()) < 0) {
            return ApiResult.fail("模板手续费不能超过" + rate.getTemplateFloat() + "%");
        }
        boolean isSuccess = templateService.updateGameTemplate(template, uid);
        return ApiResult.result(isSuccess);
    }

    /**
     * 获取模板信息
     *
     * @param id 模板编号
     * @return 返回结果
     */
    @GetMapping("/edit/{id}")
    public ApiResult<Map<String, Object>> edit(@PathVariable("id") long id) {
        if (id == 0) {
            return ApiResult.fail("参数错误");
        }
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("用户编号为空，请检查登录状态");
        }
        Map<String, Object> data = templateService.getInfo(id, uid);
        return ApiResult.success(data);
    }

    /**
     * 修改模板状态
     *
     * @param ids 模板编号
     * @param status 状态
     * @return 返回结果
     */
    @PutMapping("/status/change/{ids}")
    @OperationLog(desc = "变更游戏模板状态", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> changeStatus(@PathVariable("ids") long[] ids, Integer status) throws Exception {
        if (ids == null || ids.length == 0 || status == null) {
            return ApiResult.fail("参数错误");
        }
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("用户编号为空，请检查登录状态");
        }
        boolean isSuccess = templateService.changeStatus(ids, uid, getEnum(Status.class, status));
        if (isSuccess) {
            return ApiResult.success("操作成功");
        } else {
            return ApiResult.fail("操作失败");
        }
    }

    /**
     * 强制删除模板信息
     *
     * @param id 商户编号
     * @return 返回结果
     */
    @DeleteMapping("/delete/{id}")
    @OperationLog(desc = "删除游戏模板状态", type = OperationType.DELETE)
    public ApiResult<Map<String, Object>> delete(@PathVariable("id") long id) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("用户编号为空，请检查登录状态");
        }
        long count = gameInfoService.countByTemplateId(id);
        if (count > 0) {
            return ApiResult.fail("该模板下还有为移除的游戏，请先移除该模板下的游戏");
        }
        boolean isSuccess = templateService.delete(id, uid);
        if (isSuccess) {
            return ApiResult.success("操作成功");
        } else {
            return ApiResult.fail("操作失败");
        }
    }
}
